package pcgen.CharacterViewer;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import pcgen.android.Logger;

public class GameTypes 
{
	public GameTypes(JSONObject values)
	{
		_values = values;
	}
	
	public String getClass(String id) 
	{
		try
		{
			JSONArray values = _values.getJSONArray("gameTypes");
			for (int index = 0; index < values.length(); index++)
			{
				if (values.isNull(index))
					continue;
				
				JSONObject item = values.getJSONObject(index);
				if (!item.has("id"))
					continue;
				
				if (item.getString("id").equals(id))
				{
					String className = String.format("pcgen.CharacterViewer.resources.GameFolder%s", id);
					if (item.has("class"))
						className = item.getString("class");
					return className;
				}
			}
		}
		catch (Exception ex)
		{
			Logger.e(TAG, ex);
		}
		
		return "";
	}
	
	public GameFolderTypeList getTypes() 
	{
		GameFolderTypeList types = new GameFolderTypeList();
		
		try
		{
			JSONArray values = _values.getJSONArray("gameTypes");
			for (int index = 0; index < values.length(); index++)
			{
				if (values.isNull(index))
					continue;
				
				JSONObject item = values.getJSONObject(index);
				if (!item.has("id"))
					continue;
				
				if (item.has("active"))
				{
					boolean active = item.getBoolean("active");
					if (!active)
						continue;
				}
				
				String id = item.getString("id");
				String name = id;
				if (item.has("name"))
					name = item.getString("name");
				
				types.put(id,  name);
			}
		}
		catch (Exception ex)
		{
			Logger.e(TAG, ex);
		}
		
		return types;
	}
	
	public boolean hasGameType(String gameType) 
		throws JSONException
	{
		JSONArray values = _values.getJSONArray("gameTypes");
		for (int index = 0; index < values.length(); index++)
		{
			if (values.isNull(index))
				continue;
			
			JSONObject item = values.getJSONObject(index);
			if (!item.has("id"))
				continue;
			
			if (item.getString("id").equals(gameType))
				return true;
		}
		
		return false;
	}
	
	private JSONObject _values;
	
	private static final String TAG = GameTypes.class.getSimpleName();
	
	public class GameFolderTypeList extends HashMap<String, String>
	{
		private static final long serialVersionUID = 3061560156956826090L;
	}

}
